#include "FastEnemy.h"
#include "Patrol.h"

FastEnemy::FastEnemy()
{
	Health = FAST_ENEMY_HEALTH;
	AggroRadius = FAST_ENEMY_AGGRO;
	ChaseDistance = FAST_ENEMY_CHASE;
	EnemyBehaviours.SetMaxSpeeds(FAST_ENEMY_SPEED,FAST_ENEMY_SPEED);
	Damage = FAST_ENEMY_DAMAGE;
}

FastEnemy::~FastEnemy()
{
	SafeDelete(EnemyModel);
}

void FastEnemy::Intialise(ID3D11Device* device, 
							XMFLOAT3 position, 
							XMFLOAT3 rotation, 
							XMFLOAT3 scale,
							Terrain* gameTerrain,
							Player* player)
{
	terrain = gameTerrain;

	// Alloc memory for the enemy model
	EnemyModel = new StaticModel();

	// Load the model from file
	EnemyModel->CreateModelThroughAssimp(device, 
		std::string(FAST_ENEMY_MODEL_FILE_PATH), std::string(FAST_ENEMY_MODEL_TEXTURE_FILE_PATH),
		true, true, true, false, false);

	// Make the enemy position on the y axis equal to the terrain they are currently occupying
	position.y = (terrain->GetHeight(position.x, position.z));
	
	// Create the matrix for the enemy model.
	EnemyModel->SetWorldMatrix(position, rotation, scale);
	
	// Setup and create the hitboxes
	HitBox.CenterW = EnemyModel->GetWorldPosition();
	
	//get extents from local bounding box
	XMFLOAT3 temp = EnemyModel->GetAABBL().Extents;

	//scale extents
	temp.x *= (scale.x*2);
	temp.y *= (scale.y*2);
	temp.z *= (scale.z*2);
	
	//set hitbox extents
	HitBox.ExtentsW = temp;

	//intialise behaviours
	EnemyBehaviours.Initialise(EnemyModel->GetWorldPosition(),Velocity);

	//set player pointer
	pPlayer = player;

	//set intial state
	ChangeState(Patrol::GetInstance());
}

void FastEnemy::Update(float delta)
{
	if(IsAlive && pCurrentState)
	{
		//execute current state
		pCurrentState->Execute(this);

		//get position 
		XMFLOAT3 position = EnemyModel->GetWorldPosition();

		//add velocity to position
		position.x += Velocity.x*delta;
		position.z += Velocity.z*delta;

		//set new hight with regards to terrian
		position.y = (terrain->GetHeight(position.x, position.z)) + MODEL_Y_OFFSET;

		//set new positon
		EnemyModel->SetWorldPosition(position);
		
		//update hitbox
		HitBox.CenterW = position;
		
		//update behaviours
		EnemyBehaviours.Initialise(EnemyModel->GetWorldPosition(),Velocity);

		//if invunerable count down
		if(Invunerable)
		{
			InvunTimer -= delta;
	
			if(InvunTimer <= 0)
			{
				Invunerable = false;
			}
		}
	}

}